Skip to content

strengthen pyright detection#38

Open
robin523790 wants to merge 6 commits into
timrid:mainfrom
robin523790:feature/strengthen-pyright-detection
Open

strengthen pyright detection#38
robin523790 wants to merge 6 commits into
timrid:mainfrom
robin523790:feature/strengthen-pyright-detection

Conversation

@robin523790

@robin523790 robin523790 commented Jul 2, 2026

Copy link
Copy Markdown
import construct as cs
import construct_typed as cst

@dataclasses.dataclass
class MyClass(cst.DataclassMixin):
    my_param: bytes = cst.csfield(cs.Bytes(30), doc="""test""")

c = MyClass()

Above code did not trigger a Pyright error but failed at runtime.

This PR introduces @overloads to csfield() that allow Pyright to ensure that:

  • fields that are not provided now raise errors
  • Const fields that are provided now raise errors (as these must only be provided in the declaration)
  • Default fields can be provided but do not have to (as the default value may be overriden)

This PR includes the migration to uv and the version bump to 0.8.0 found here: #37.

BREAKING CHANGES
This PR introduces breaking changes, thus the version has been bumped to 0.8.0.

  • Default fields are only treated as optional if they have an explicit (and redundant) default= marker. A wrapper method csdefault_field() has been provided for convenience.
class MyClass(cst.DataclassMixin):
    my_default_1: bytes = cst.csfield(cs.Default(cs.Bytes(30), bytes(30)), doc="""test""", default=bytes(30))
    my_default_2: bytes = cst.csdefault_field(cs.Bytes(5), bytes(5), doc="""test""")
  • Any fields following a Default field must have the kw_only= marker and must be provided by keyword.
class MyClass(cst.DataclassMixin):
    my_default: bytes = cst.csdefault_field(cs.Bytes(30), bytes(30), doc="""test""")
    my_param: bytes = cst.csfield(cs.Bytes(5), doc="""test""", kw_only=True)

c1 = MyClass(b"Hello")  # WRONG
c2 = MyClass(my_param=b"World")  # correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants